<template>
    <h1>按下左侧菜单拖拽至内容</h1>
    <div class="box">
        <div class="drag">
            <div class="drag-item">
                <div class="drag-item-li" draggable="true" @dragstart="onDragstart($event, item)"
                    v-for="item, index in list" :key="'drag-item-li' + index">
                    <span>{{ item.name }}</span>
                </div>
            </div>
            <div class="drag-container" @drop="onDrop" @dragenter="preventDefault" @dragover="preventDefault"
                @dragleave="preventDefault">
                <div class="drag-container-box">
                    <div class="drag-container-box-item" v-for="book, key in bookList"
                        :key="'drag-container-box-item' + key">
                        <div class="drag-container-box-item-header">
                            <span class="dcbih-lookName">
                                《{{ book.name }}》
                            </span>
                            <span class="dccbih-lookAuthor">
                                作者:{{ book.author }}
                            </span>
                        </div>
                        <div class="drag-container-box-item-text">
                            <span v-html="book.tip" v-if="key == 0">
                            </span>
                            <span v-else>
                                <sa-text-toggle :text="book.tip" :width="'100%'" :maxLines="2">
                                    <template v-slot:default="{ clickToggle, expanded }">
                                        <a @click="clickToggle" class="btn">
                                            {{ expanded ? "收起" : "展开" }}
                                        </a>
                                    </template>
                                </sa-text-toggle>
                            </span>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <demo></demo>
</template>

<script lang="ts">
import demo from './demo2.vue';
export default {
    components:{demo},
    data() {
        return {
            list: [
                { name: "灵境行者" },
                { name: "机武风暴" },
                { name: "夜的命名术" },
            ],
            book: {
                "灵境行者": {
                    tip: `亘古通今，传闻世有灵境。关于灵境的说法，历朝历代的名人雅士众说纷纭，诗中记载：“自齐至唐，兹山濅荒，灵境寂寥，罕有人游。”“灵境不可状，鬼工谅难求。”`,
                    author: "卖报小郎君"
                },
                "机武风暴": {
                    tip: `太阳系联盟为探究灵魂奥秘，秘密组建天才组织——天启少年班，集合了来自太阳系四大联邦天赋异禀的年轻人，但在研究途中出现重大事故，多人伤亡，首犯被监禁，少年班解散，档案封存成为禁忌。五年后，在泛太阳系机甲大赛上铩羽而归的天京机武A级军事学院，如同天上掉馅饼般的迎来了亚洲区机甲预备班第一名的瞳神，开始了重建之路，然而这只是开始，不可思议的事情开始频繁出现，旋涡中心却是一个默默无闻的二年生。`,
                    author: '骷髅精灵'
                },
                "夜的命名术": {
                    tip: `蓝与紫的霓虹中，浓密的钢铁苍穹下，数据洪流的前端，是科技革命之后的世界，也是现实与虚幻的分界。钢铁与身体，过去与未来。这里，表世界与里世界并存，面前的一切，像是时间之墙近在眼前。黑暗逐渐笼罩。可你要明白啊我的朋友，我们不能用温柔去应对黑暗，要用火。`,
                    author: "会说话的肘子"
                },
            },
            active: {
                itemDetail: {},
                dragged: {
                    x: null,
                    y: null
                }
            },
            bookList: [
                {
                    name: "牛马",
                    tip: `拖拽元素到指定dom的方法 <a target='_blank' href='https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes/draggable '>draggable</a> <a target='_blank' href='https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Drag_operations '>事件api</a>`,
                    author: "牛马"
                }
            ]
        }
    },
    methods: {
        onDragstart(e, item) {
            this.active.itemDetail = item;
            this.active.dragged.x = e.offsetX;
            this.active.dragged.y = e.offsetY;
        },
        onDrop(e) {
            let book = { ...this.book[this.active.itemDetail.name], name: this.active.itemDetail.name };
            this.bookList.push(book);
            e.stopPropagation();
            e.preventDefault();
            return false;
        },
        preventDefault(e) {
            e.preventDefault()
        }
    }
}
</script>

<style scoped lang="less">
.btn {
    color: #409eff;
}

.box {
    width: 100%;
    display: flex;
}

.drag {
    width: 100%;
    margin: auto;
    display: flex;

    .drag-item {
        width: 200px;
        border: 1px solid #faf1fa;
        box-sizing: border-box;
        padding: 10px 10px;

        .drag-item-li {
            width: 100%;
            height: 32px;
            line-height: 32px;
            border: 1px solid #eee;
            text-align: center;
            font-size: 14px;
            border-radius: 5px;
            user-select: none;
            cursor: move;
            background-color: white;
        }

        .drag-item-li:hover {
            border-style: dashed solid double dashed;
            box-shadow: 0px 0px 4px #409eff;
            border-color: #409eff;
        }

        .drag-item-li+.drag-item-li {
            margin-top: 10px;
        }
    }

    .drag-container {
        flex: 1;
        margin-left: 10px;
        background-color: white;

        .drag-container-box {
            width: 100%;
            height: 100%;
            position: relative;
            box-sizing: border-box;
            padding: 20px;
            box-sizing: border-box;
            border: 1px solid #faf1fa;
            overflow: auto;

            .drag-container-box-item {
                width: 100%;
                height: auto;
                min-height: 32px;
                background-color: white;
                box-sizing: border-box;
                padding: 14px;
                border-radius: 10px;
                box-shadow: 0px 0px 5px #ebeef5;

                .drag-container-box-item-header {
                    font-size: 15px;
                    height: 20px;
                    line-height: 20px;
                    display: flex;

                    span+span {
                        margin-left: .5em;
                    }

                    span {
                        color: #333;
                    }
                }

                .drag-container-box-item-text {
                    margin-top: 10px;
                    font-size: 15px;
                    line-height: 1.5em;
                    text-indent: 2em;
                    color: #a6a6a6;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 3;
                    -webkit-box-orient: vertical;

                    span {
                        white-space: pre-line;
                    }

                    /deep/a {
                        font-size: 14px;
                        color: #a6a6a6;
                    }

                }
            }

            .drag-container-box-item+.drag-container-box-item {
                margin-top: 10px;
            }
        }
    }
}
</style>